package com.hex.ds.hdrs.period.task.service;

import cn.hutool.core.util.StrUtil;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.common.page.PageData;
import com.hex.ds.hdrs.period.task.converter.IMutexPoolGroupConverter;
import com.hex.ds.hdrs.period.task.dao.MutexPoolGroupDao;
import com.hex.ds.hdrs.period.task.dto.MutexPoolDto;
import com.hex.ds.hdrs.period.task.po.MutexPoolGroup;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 互斥池管理表 服务实现类
 * </p>
 *
 * @author tangji
 * @since 2021-01-21
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class MutexPoolGroupService {

    private final MutexPoolGroupDao mutexPoolGroupDao;

    private final MutexPoolService mutexPoolService;

    private final IMutexPoolGroupConverter mutexPoolGroupConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [mutexPoolGroup, page] <br>
     * @Return: com.hex.ds.hdrs.common.page.PageData <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/5 18:44 <br>
     * @Version： V2.0.2<br>
     */
    public PageData queryListByPage(MutexPoolGroup mutexPoolGroup, Page page) {
        List<MutexPoolGroup> mutexPoolGroups = mutexPoolGroupDao.queryListByPage(mutexPoolGroup, page);
        return new PageData(mutexPoolGroups, page);
    }

    /*
     * @Method: add <br>
     * @Param: [mutexPoolDto] <br>
     * @Return: com.hex.ds.hdrs.period.task.po.MutexPoolGroup <br>
     * @Description：新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/5 18:44 <br>
     * @Version： V2.0.2<br>
     */
    public MutexPoolGroup add(MutexPoolDto mutexPoolDto) {
        String appCodes = StrUtil.join(",", mutexPoolDto.getAppCodes());
        MutexPoolGroup mutexPoolGroup = mutexPoolGroupConverter.toMutexPoolGroup(appCodes, mutexPoolDto);
        mutexPoolGroupDao.insert(mutexPoolGroup);
        return mutexPoolGroup;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void modifyById(MutexPoolDto mutexPoolDto) {
        MutexPoolGroup beforMutexPoolGroup = queryByGroupCode(mutexPoolDto.getGroupCode());
        String appCodes = StrUtil.join(",", mutexPoolDto.getAppCodes());
        MutexPoolGroup mutexPoolGroup = mutexPoolGroupConverter.toMutexPoolGroup(mutexPoolDto, appCodes);
        mutexPoolGroupDao.update(mutexPoolGroup);
        updateAppSession(beforMutexPoolGroup, mutexPoolDto.getAppCodes());
    }

    private void updateAppSession(MutexPoolGroup beforMutexPoolGroup, List<String> appCodeList) {
        appCodeList.addAll(Arrays.asList(StrUtil.split(beforMutexPoolGroup.getAppCodes(), ",")));
        for (String appCode : appCodeList.stream().distinct().collect(Collectors.toList())) {
            mutexPoolService.updateMutexTaskForAppSeesion(appCode);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void batcheRemove(List<String> groupCodes) {
        if (CollectionUtils.isNotEmpty(groupCodes)) {
            for (String groupCode : groupCodes) {
                mutexPoolGroupDao.removeByGroupCode(groupCode);
                mutexPoolService.removeByGroupCode(groupCode);
            }
        }
    }

    public MutexPoolGroup queryByGroupCode(String groupCode) {
        return mutexPoolGroupDao.queryByGroupCode(groupCode);
    }

    public boolean checkGroupCode(MutexPoolGroup mutexPoolGroup) {
        MutexPoolGroup dbMutexPoolGroup = mutexPoolGroupDao.queryByGroupCode(mutexPoolGroup.getGroupCode());
        return dbMutexPoolGroup != null && !dbMutexPoolGroup.getPkId().equals(mutexPoolGroup.getPkId());
    }
}
